﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

using Perseus;
using Perseus.Data;

namespace Hetymine.Executioner.Plugins.Imgur {
    /// <summary>
    /// Interaction logic for ImguConfig.xaml
    /// </summary>
    public partial class ImgurConfig : UserControl, IConfigurator<IniFile> {
        private IniFile _Config;

        public ImgurConfig() {
            InitializeComponent();

            this.cfgImageDirectory.TextChanged += new TextChangedEventHandler(ImageDirectory_TextChanged);
            this.cfgCopyToClipboard.Click += new RoutedEventHandler(CopyToClipboard_Click);
            this.cfgCopyUrl.SelectionChanged += new SelectionChangedEventHandler(CopyUrl_SelectionChanged);
            this.cfgOpenUrlInBrowser.Click += new RoutedEventHandler(OpenUrlInBrowser_Click);
            this.cfgOpenUrl.SelectionChanged += new SelectionChangedEventHandler(OpenUrl_SelectionChanged);
            this.cfgLogImgurPage.Click += new RoutedEventHandler(LogUrls_Click);
            this.cfgLogOrigionalImage.Click += new RoutedEventHandler(LogUrls_Click);
            this.cfgLogLargeThumbnail.Click += new RoutedEventHandler(LogUrls_Click);
            this.cfgLogSmallThumbnail.Click += new RoutedEventHandler(LogUrls_Click);
            this.cfgLogDeletePage.Click += new RoutedEventHandler(LogUrls_Click);
        }

        private void ImageDirectory_TextChanged(object sender, TextChangedEventArgs e) {            
            this._Config.SetString("Plugins.Imgur", "Default Image Directory", this.cfgImageDirectory.Text.Trim());
        }
        private void CopyToClipboard_Click(object sender, RoutedEventArgs e) {
            this._Config.SetBool("Plugins.Imgur", "Copy To Clipboard", this.cfgCopyToClipboard.IsChecked.Value);
        }
        private void CopyUrl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            this._Config.SetInt("Plugins.Imgur", "Copy To Clipboard Page", this.cfgCopyUrl.SelectedIndex);
        }
        private void OpenUrlInBrowser_Click(object sender, RoutedEventArgs e) {
            this._Config.SetBool("Plugins.Imgur", "Open Url In Browser", this.cfgOpenUrlInBrowser.IsChecked.Value);
        }
        private void OpenUrl_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            this._Config.SetInt("Plugins.Imgur", "Open Url In Browser Page", this.cfgOpenUrl.SelectedIndex);
        }
        private void LogUrls_Click(object sender, RoutedEventArgs e) {
            int log = 0;

            // Store logged urls as a flag
            if (this.cfgLogImgurPage.IsChecked.Value) {
                log |= 1;
            }
            if (this.cfgLogOrigionalImage.IsChecked.Value) {
                log |= 2;
            }
            if (this.cfgLogLargeThumbnail.IsChecked.Value) {
                log |= 4;
            }
            if (this.cfgLogSmallThumbnail.IsChecked.Value) {
                log |= 8;
            }
            if (this.cfgLogDeletePage.IsChecked.Value) {
                log |= 16;
            }

            this._Config.SetInt("Plugins.Imgur", "Log Pages", log);
        }

        #region IConfigurator<IniFile> Members
        public void InitializeConfig(IniFile config) {
            this._Config = config;

            this.cfgImageDirectory.Text = this._Config.GetString(
                "Plugins.Imgur",
                "Default Image Directory",
                Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)
            );

            this.cfgCopyToClipboard.IsChecked = this._Config.GetBool(
                "Plugins.Imgur",
                "Copy To Clipboard",
                true
            );
            this.cfgCopyUrl.SelectedIndex = this._Config.GetInt(
                "Plugins.Imgur",
                "Copy To Clipboard Page",
                0
            );


            this.cfgOpenUrlInBrowser.IsChecked = this._Config.GetBool(
                "Plugins.Imgur",
                "Open Url In Browser",
                false
            );
            this.cfgOpenUrl.SelectedIndex = this._Config.GetInt(
                "Plugins.Imgur",
                "Open Url In Browser Page",
                0
            );

            int log = this._Config.GetInt(
                "Plugins.Imgur",
                "Log Pages",
                0
            );

            this.cfgLogImgurPage.IsChecked = ((log | 1) == log);
            this.cfgLogOrigionalImage.IsChecked = ((log | 2) == log);
            this.cfgLogLargeThumbnail.IsChecked = ((log | 4) == log);
            this.cfgLogSmallThumbnail.IsChecked = ((log | 8) == log);
            this.cfgLogDeletePage.IsChecked = ((log | 16) == log);
        }

        public UserControl ConfigPanel { get { return this; } }
        #endregion
    }
}
